use fastmcp_rs::{
    FastMcpServer,
    mcp_register_resources,
    mcp_resource_static,
    mcp_resource_dynamic,
    resource::ResourceContent,
};
use serde_json::json;

// 将资源定义抽离至模块顶层，便于复用与测试
fn hello_resource() -> fastmcp_rs::resource::ResourceDefinition {
    mcp_resource_static! {
        uri: "resource://hello",
        content: ResourceContent::text("hello world"),
        description: "Static hello text",
        annotations: { "kind": json!("demo") },
    }
}

fn time_resource() -> fastmcp_rs::resource::ResourceDefinition {
    mcp_resource_dynamic! {
        uri: "resource://time",
        resolver: |ctx: fastmcp_rs::resource::ResourceContext| async move {
            let value = json!({
                "uri": ctx.uri,
                "request_id": ctx.request_id,
                "timestamp": ctx.timestamp.to_rfc3339(),
            });
            Ok(ResourceContent::json(value))
        },
        description: "Current server time",
    }
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    tracing_subscriber::fmt()
        .with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
        .init();

    let server = FastMcpServer::builder()
        .name("ResourcesDemo")
        .instructions("Static and dynamic resources")
        .build()
        .into_shared();

    mcp_register_resources!(server, [ hello_resource(), time_resource() ]);

    // Read both resources to demonstrate resolution
    let hello = server.read_resource("resource://hello").await?;
    let time = server.read_resource("resource://time").await?;
    tracing::info!("hello={:?}", hello);
    tracing::info!("time={:?}", time);

    Ok(())
}